import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue' //引入login组件
import Home from '../components/Home.vue' //引入首页
import Welcome from '../components/Welcome.vue'//引入欢迎页面
import UserList from '../components/admin/UserList.vue'//引入用户列表

Vue.use(VueRouter)

const routes = [
  //设置访问路径
  {
    path:"/",
    redirect:"/login"
  },
  {
    path:"/login",
    component: Login
  },
  {
    path:"/home",
    component: Home,
    redirect:"/welcome",
    children:[
      {path:"/welcome",component: Welcome,},
      {path:"/user",component:UserList,}
    ]
  }

]

const router = new VueRouter({
  routes
})
//挂载全局路由导航守卫（这里类似拦截器设置+Login.vue里就有一个方法是验证登录失败的=双重监控）
router.beforeEach((to,from,next)=>{
  //to：将要访问
  //from：从哪访问
  //next：接着干的事 
  //next(url) 重定向到url上 ; next() 继续访问to路径
  if(to.path=="/login")return next();
  //获取user
  const userFlag = window.sessionStorage.getItem("user");//取出当前用户
  if(!userFlag)return next("/login");//无值，返回登录页面
  next();//符合要求 放行

})

export default router //暴露出去
